package com.health_predict_springboot.controller;

import com.health_predict_springboot.pojo.SysExportRecord;
import com.health_predict_springboot.service.SysExportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/export-record")
public class SysExportRecordController {

    @Autowired
    private SysExportRecordService sysExportRecordService;

    @GetMapping("/{id}")
    public SysExportRecord getExportRecordById(@PathVariable("id") Integer id) {
        return sysExportRecordService.getExportRecordById(id);
    }

    @GetMapping("/user/{userId}")
    public List<SysExportRecord> getExportRecordsByUserId(@PathVariable("userId") Integer userId) {
        return sysExportRecordService.getExportRecordsByUserId(userId);
    }

    @GetMapping("/all")
    public List<SysExportRecord> getAllExportRecords() {
        return sysExportRecordService.getAllExportRecords();
    }

    @PostMapping
    public boolean addExportRecord(@RequestBody SysExportRecord exportRecord) {
        exportRecord.setExportTime(new Date());
        return sysExportRecordService.addExportRecord(exportRecord);
    }

    @PutMapping
    public boolean updateExportRecord(@RequestBody SysExportRecord exportRecord) {
        return sysExportRecordService.updateExportRecord(exportRecord);
    }

    @DeleteMapping("/{id}")
    public boolean deleteExportRecord(@PathVariable("id") Integer id) {
        return sysExportRecordService.deleteExportRecord(id);
    }

    @GetMapping("/page")
    public List<SysExportRecord> getExportRecordsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) String exportType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        Map<String, Object> params = Map.of(
                "userId", userId,
                "exportType", exportType,
                "status", status,
                "startTime", startTime,
                "endTime", endTime,
                "offset", (pageNum - 1) * pageSize,
                "pageSize", pageSize
        );
        return sysExportRecordService.getExportRecordsByPage(params);
    }

    @GetMapping("/count")
    public int getExportRecordCount(
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) String exportType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        Map<String, Object> params = Map.of(
                "userId", userId,
                "exportType", exportType,
                "status", status,
                "startTime", startTime,
                "endTime", endTime
        );
        return sysExportRecordService.getExportRecordCount(params);
    }
}